<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/banner/List' }">
                内容管理</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/column/List' }">
                栏目管理</el-breadcrumb-item
            >
            <el-breadcrumb-item>添加栏目</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="添加栏目"> </el-page-header>
            <el-form
                :model="addForm"
                :rules="addFormRules"
                ref="addFormRef"
                label-width="120px"
                hide-required-asterisk
            >
                <el-form-item label="父级栏目" align="left">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="不选择则栏目为顶级栏目"
                        placement="top"
                    >
                        <select-tree
                            ref="setTree"
                            @subSelectData="setParentId"
                        ></select-tree>
                    </el-tooltip>
                </el-form-item>
                <el-form-item label="栏目名称" prop="name">
                    <el-input
                        v-model="addForm.name"
                        placeholder="请输入栏目名称"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="栏目图标" prop="icon">
                    <el-input
                        v-model="addForm.icon"
                        placeholder="请输入栏目图标"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="栏目类型" prop="target_type" align="left">
                    <el-select
                        v-model="addForm.target_type"
                        placeholder="请选择栏目类型"
                    >
                        <el-option
                            v-for="item in optionsType"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="文章分类id"
                    prop="targey_content"
                    v-if="addForm.target_type == 1"
                >
                    <el-input
                        v-model="addForm.targey_content"
                        placeholder="请输入文章分类id"
                    />
                </el-form-item>
                <el-form-item
                    label="文章id"
                    prop="targey_content"
                    v-if="addForm.target_type == 2"
                >
                    <el-input
                        v-model="addForm.targey_content"
                        placeholder="请输入文章id"
                    />
                </el-form-item>
                <el-form-item
                    label="单页模板名称"
                    prop="targey_content"
                    v-if="addForm.target_type == 3"
                >
                    <el-input
                        v-model="addForm.targey_content"
                        placeholder="请输入单页模板名称"
                    />
                </el-form-item>
                <el-form-item
                    label="外链地址"
                    prop="targey_content"
                    v-if="addForm.target_type == 4"
                >
                    <el-input
                        v-model="addForm.targey_content"
                        placeholder="请输入外链地址"
                    />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input
                        type="textarea"
                        autosize
                        v-model="addForm.remarks"
                        placeholder="请输入内容"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="addForm.sort" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="addForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item align="left">
                    <el-button type="primary" @click="subAdd">保存</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import SelectTree from '@/components/select_tree/SelectTree.vue'
export default {
    components: { SelectTree },
    data () {
        return {
            addForm: {
                parent_id: 0,
                name: '',
                icon: '',
                target_type: '',
                targey_content: '',
                remarks: '',
                sort: 10,
                status: 1
            },
            optionsType: [
                { value: '1', label: '文章分类' },
                { value: '2', label: '文章' },
                { value: '3', label: '单页' },
                { value: '4', label: '外链' }
            ],
            // 添加表单验证规则对象 tag value_type
            addFormRules: {
                name: [
                    { required: true, message: '请输入栏目名称', trigger: 'change' }
                ],
                target_type: [
                    { required: true, message: '请选择栏目类型', trigger: 'blur' }
                ],
                targey_content: [
                    { required: true, message: '请输入目标类型内容', trigger: 'blur' }
                ]
            }
        }
    },
    mounted () {
        this.getColumnTree()
    },
    methods: {
        setParentId (parentId) {
            this.addForm.parent_id = parentId
        },
        getColumnTree () {
            requestApis.getColumnTree().then(res => {
                if (res.meta.code === 1) {
                    this.$refs.setTree.setSelectData(res.data)
                } else {
                    this.mg({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        goBack () {
            this.$router.back()
        },
        subAdd () {
            this.$refs.addFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveColumnAdd(this.addForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        },
        resetForm () {
            this.$refs.addFormRef.resetFields()
            this.addForm.status = 1
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
